package com.ruoyi.web.api.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.AppUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysDevice;
import com.ruoyi.system.domain.vo.DeviceListVo;
import com.ruoyi.system.service.ISysDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备管理操作处理
 *
 * @author ruoyi
 */
@Api(tags = "设备管理")
@RestController
@RequestMapping("/system/device")
public class SysDeviceController extends BaseController {

    private final ISysDeviceService sysDeviceService;

    public SysDeviceController(ISysDeviceService sysDeviceService) {
        this.sysDeviceService = sysDeviceService;
    }

    /**
     * 获取设备列表
     */
    @ApiOperation(value = "获取设备列表", notes = "获取所有设备信息")
    @GetMapping("/list")
    public TableDataInfo list(SysDevice sysDevice) {
        startPage();
        List<DeviceListVo> list = sysDeviceService.selectSysDeviceList(sysDevice);
        return getDataTable(list);
    }

    /**
     * 根据设备编号获取详细信息
     */
    @ApiOperation(value = "获取设备详细信息", notes = "根据id获取设备详细信息")
    @ApiImplicitParam(name = "deviceId", value = "设备ID", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    @GetMapping(value = "/{deviceId}")
    public AjaxResult getInfo(@PathVariable String deviceId) {
        return success(sysDeviceService.selectSysDeviceById(deviceId));
    }

    /**
     * 根据设备编号获取详细信息
     */
    @ApiOperation(value = "根据用户获取设备详细信息", notes = "根据用户获取设备详细信息")
    @GetMapping(value = "/getByUser")
    public AjaxResult getInfo() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        AppUser user = loginUser.getAppUser();
        Long userId = user.getId();
        return success(sysDeviceService.selectDeviceByUserId(userId));
    }


    @ApiOperation(value = "手环绑定用户", notes = "手环绑定用户")
    @GetMapping("/bind")
    public AjaxResult bindDevice(@RequestParam String deviceId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        boolean result = sysDeviceService.bindDeviceToUser(deviceId, userId);
        return result ? AjaxResult.success("绑定成功") : AjaxResult.error("绑定失败");
    }

    @ApiOperation(value = "手环解绑", notes = "手环解绑")
    @GetMapping("/unbind")
    public AjaxResult unbindDevice(@RequestParam String deviceId) {
        boolean result = sysDeviceService.unbindDevice(deviceId);
        return result ? AjaxResult.success("解绑成功") : AjaxResult.error("解绑失败");
    }
}